using System;
using System.Diagnostics.Contracts;
using System.Collections.Generic;
using System.Linq;

namespace ReactiveLang
{
    using Util;

    class SubjectBase<TObserver>
    {
        LinkedList<TObserver> _observers = new LinkedList<TObserver>();

        public IDisposable Activate(TObserver observer)
        {
            Contract.Requires<ArgumentNullException>(observer != null, "observer");
            var node = _observers.AddLast(observer);
            return Disposable.Create(() => node.List.Remove(node));
        }

        protected void ForEach(Action<TObserver> action)
        {
            Contract.Requires(action != null);
            foreach(var o in _observers.ToList())
                action(o);
        }
    }
}